package com.ejuornal.dao.impl;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.List;

import org.springframework.jdbc.core.RowMapper;
import org.springframework.stereotype.Component;

import com.ejuornal.bean.SystemInfo;
import com.ejuornal.dao.SystemInfoDao;

/**
 * 系统信息
 * 
 * @author 石亚博
 * 
 */
@Component("systemInfoDao")
public class SystemInfoDaoImpl extends DaoSupport<SystemInfo> implements
		SystemInfoDao {

	public void add(String sql, Object... objs) {
		super.add(sql, objs);
	}

	public void update(String sql, Object... objs) {
		super.update(sql, objs);
	}

	public SystemInfo findById(int systemInfoId) {
		String sql = "SELECT * FROM t_systemInfo WHERE systemId = ?";
		SystemInfo systemInfo = super.getJdbcTemplate().queryForObject(sql,
				new Object[] { systemInfoId }, new RowMapper<SystemInfo>() {
					public SystemInfo mapRow(ResultSet rs, int rowNum)
							throws SQLException {
						SystemInfo systemInfo = new SystemInfo();
						systemInfo.setSystemId(rs.getInt("systemId"));
						systemInfo.setItemName(rs.getString("itemName"));
						systemInfo.setItemValue(rs.getString("itemValue"));

						return systemInfo;
					}
				});

		return systemInfo;
	}

	public SystemInfo findByName(String itemName) {
		String sql = "SELECT * FROM t_systemInfo WHERE itemName = ?";
		SystemInfo systemInfo = super.getJdbcTemplate().queryForObject(sql,
				new Object[] { itemName }, new RowMapper<SystemInfo>() {
					public SystemInfo mapRow(ResultSet rs, int rowNum)
							throws SQLException {
						SystemInfo systemInfo = new SystemInfo();
						systemInfo.setSystemId(rs.getInt("systemId"));
						systemInfo.setItemName(rs.getString("itemName"));
						systemInfo.setItemValue(rs.getString("itemValue"));

						return systemInfo;
					}
				});

		return systemInfo;
	}
	
	public List<SystemInfo> findAll() {
		String sql = "SELECT * FROM t_systeminfo";
		List<SystemInfo> systemInfoList = super.getJdbcTemplate().query(sql,
				new RowMapper<SystemInfo>() {

					public SystemInfo mapRow(ResultSet rs, int rowNum)
							throws SQLException {
						SystemInfo systemInfo = new SystemInfo();
						systemInfo.setSystemId(rs.getInt("systemId"));
						systemInfo.setItemName(rs.getString("itemName"));
						systemInfo.setItemValue(rs.getString("itemValue"));
						
						return systemInfo;
					}
				});

		return systemInfoList;
	}
	
}
